package com.example.myapplication5;

import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;

import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.TextPaint;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.text.style.ForegroundColorSpan;
import android.text.style.ImageSpan;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;


public class MainActivity extends AppCompatActivity {

    private ActionBar ToastUtil;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        jpk();
        ced();
    }
    private void jpk(){
        TextView ab= findViewById(R.id.pa);
        SpannableStringBuilder ac =new SpannableStringBuilder();
        ac.append("     .                                                            .    .       ");
        ForegroundColorSpan ad = new ForegroundColorSpan(Color.parseColor("#FFFF0000"));

        float dp = 25;
        final float scale = getResources().getDisplayMetrics().density;
        int px = (int) (dp * scale + 0.5f);
        Drawable drawable = getResources().getDrawable(R.drawable.chehui);
        drawable.setBounds(0, 0, px, px);
        ImageSpan imageSpan = new ImageSpan(drawable);
        ac.setSpan(imageSpan, 5,6, Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
        ab.setText(ac);

        float dp1 = 25;
        final float scale1 = getResources().getDisplayMetrics().density;
        int px1 = (int) (dp1 * scale1 + 0.5f);
        Drawable drawable1 = getResources().getDrawable(R.drawable.dianzan);
        drawable1.setBounds(0, 0, px1, px1);
        ImageSpan imageSpan1 = new ImageSpan(drawable1);
        ac.setSpan(imageSpan1, ac.length()-13,ac.length()-12,Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
        ab.setText(ac);

        float dp2 = 25;
        final float scale2 = getResources().getDisplayMetrics().density;
        int px2 = (int) (dp2 * scale2 + 0.5f);
        Drawable drawable2 = getResources().getDrawable(R.drawable.xinxi);
        drawable2.setBounds(0, 0, px2, px2);
        ImageSpan imageSpan2 = new ImageSpan(drawable2);
        ac.setSpan(imageSpan2, ac.length()-8,ac.length()-7, Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
        ab.setText(ac);

        //点击跳转上一界面

    }
   private void ced(){
       TextView ab= findViewById(R.id.pb);
       SpannableStringBuilder ac =new SpannableStringBuilder();
       ac.append("...等觉得你的内容很赞。");
       ForegroundColorSpan ad = new ForegroundColorSpan(Color.parseColor("#FFFF0000"));
       //ac.setSpan(ad,0,1,Spanned.SPAN_EXCLUSIVE_INCLUSIVE);
       ab.setText(ac);

       //25dp转化为px
       float dp = 25;
       final float scale = getResources().getDisplayMetrics().density;
       //由25dp转化来的px
       int px = (int) (dp * scale + 0.5f);

       Drawable drawable = getResources().getDrawable(R.drawable.dianzan);
       drawable.setBounds(0, 0, px, px);
       ImageSpan imageSpan = new ImageSpan(drawable);
       ac.setSpan(imageSpan, ac.length()-1,ac.length(), Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
       ab.setText(ac);

       ClickableSpan clickableSpan = new ClickableSpan() {

           @Override
           public void updateDrawState(TextPaint ds) {
               super.updateDrawState(ds);
               ds.setColor(Color.parseColor("#FF0090FF"));//设置颜⾊
           }

           @Override
           public void onClick(@NonNull View view) {
               Toast.makeText(MainActivity.this,"点赞成功",Toast.LENGTH_SHORT).show();
           }
       };
       ac.setSpan(clickableSpan, ac.length()-1,ac.length(), Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
       ab.setHighlightColor(Color.TRANSPARENT); //设置点击后的颜⾊为透明，否则会⼀直出现⾼亮
       ab.setText(ac);
       ab.setMovementMethod(LinkMovementMethod.getInstance());//触发点击事件

   }


}
